# Filters added to this controller apply to all controllers in the application.
# Likewise, all the methods added will be available for all controllers.

class ApplicationController < ActionController::Base
  # Pick a unique cookie name to distinguish our session data from others'
  session :session_key => '_crowd-control_session_id'
  helper :all
  before_filter :set_debug_session
  
  include AuthenticatedSystem
  before_filter :login_from_cookie

  layout 'default'
  
  before_filter :ensure_installed, :except => [:install, :style]
  before_filter :reload_localizations
  
  around_filter do |controller, action|
    begin
      action.call
    rescue ActiveRecord::RecordNotFound => e
      raise e if RAILS_ENV == 'development'
      controller.not_found
    end
  end
  
  def ensure_installed
    if User.no_managers_exist?
      redirect_to :controller => 'welcome', :action => 'install'
      return false
    end
  end
  
  def reload_localizations
    Globalite.load_localization! if RAILS_ENV == 'development'
  end
  
  def not_found
    render :status => 404, :file => 'public/404.html'
  end
  
  private
  def set_debug_session
    if RAILS_ENV == 'development' && params[:debug_session]
      session[:debug_session] = params[:debug_session] == 'true' ? true : false
    end
  end
  
end
